var material_model = null;
var information_type = null;
var voltage_type = null;
var buy_type = null;
var updateGuid = GetUrlParam("updateGuid");
var modelGuid = GetUrlParam("modelGuid");

$(function () {
    var lastsel;
    $.when(
    	getdatadictionary("material_model"),
        getdatadictionary("information_type"),
    	getdatadictionary("voltage_type"),
    	getdatadictionary("buy_type")
    ).done(
        $("#jqGrid").jqGrid({
            url: baseURL + 'bss/dmaterialmodelitem/list?modelGuid='+modelGuid,
            datatype: "json",
            colModel: [
                { label: 'guid', name: 'guid', index: 'guid', width: 50, key: true,hidden:true },
                { label: '名称', name: 'modelGuid', index: 'model_guid', width: 80 },
                { label: '参数', name: 'materialGuid', index: 'material_guid', width: 80 },
                { label: '数量', name: 'count', index: 'count', width: 80,editable: true,edittype: "text"},
                { label: '排序', name: 'paixu', index: 'paixu', width: 80,editable: true,edittype: "text"},

                { label: '', name: 'datastate', index: 'datastate', width: 80,hidden:true },
                { label: '操作', name: '', width: 180,
                    formatter: function (cellvalue, options, rowObject) {
                        return "<a  class='btn btn-primary' onclick='updThisNum(\"" + rowObject.guid + "\",\"" + rowObject.datastate + "\")'>更换</a>"
                    }
                }
            ],
            viewrecords: true,
            height: 385,
            rowNum: 10,
            rowList : [10,30,50],
            rownumbers: true,
            rownumWidth: 25,
            autowidth:true,
            onSelectRow :function(id) { //  修改数量
                if (id && id !== lastsel) {
                    jQuery('#jqGrid').jqGrid('restoreRow', lastsel);
                    jQuery('#jqGrid').jqGrid('editRow', id, true);
                    lastsel = id;
                }
            },
            editurl : baseURL  + "bss/dmaterialmodelitem/updCount",
            multiselect: true,
            pager: "#jqGridPager",
            jsonReader : {
                root: "page.list",
                page: "page.currPage",
                total: "page.totalPage",
                records: "page.totalCount"
            },
            prmNames : {
                page:"page",
                rows:"limit",
                order: "order"
            },
            gridComplete:function(){
                //隐藏grid底部滚动条
                $("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
            }
        })
	)
});

//  保存材料
function saveMaterial() {

    var obj = $("#jqGrid");
    var rowIds = obj.getDataIDs();
    var arrayData = "";
    if (rowIds.length > 0) {
        for (var i = 0; i < rowIds.length; i++) {
            arrayData += obj.getRowData(rowIds[i]).materialGuid+"("+obj.getRowData(rowIds[i]).count+");"+"\n";
        }
    }

    var url = "bss/dmaterialmodel/update";
    var data22 = {
        "guid":modelGuid ,
        "content":arrayData
    };
    $.ajax({
        type: "POST",
        url: baseURL + url,
        contentType: "application/json",
        data: JSON.stringify(data22),
        success: function(r){
            if(r.code === 0){
                alert('操作成功', function(index){
                    // vm.reload();
                    parent.location.reload();
                });
            }else{
                alert(r.msg);
            }
        }
    });
}


//  更换
function updThisNum(guid,mBigtype) {
    var title ="请选择材料类型";
    var content = "dmaterialmodelChoose.html?modelGuid="+modelGuid+"&mBigtype="+mBigtype+"&delId="+guid+"&Y=y";
    layer.open({
        id:"modelChoose",
        type: 2,
        title: title,
        shadeClose: true,
        shade: 0.5,
        maxmin: true, //开启最大化最小化按钮
        area: ['100%', '100%'],
        content: content,
        success: function(layero,index){
            var body = layer.getChildFrame('body', index);
            var iframeWin = window[layero.find('iframe')[0]['name']];
            iframeWin.chooseBigtype();
            // layer.close(index);
        }
    });
}

//  数据回调
var allInfo = null;
var getBack = function () {
    var data = allInfo;
    return data;
}


//	添加模块
function addModel() {
    var title ="请选择材料类型";
    var content = "dmaterialmodelChoose.html?modelGuid="+modelGuid;
    layer.open({
        id:"modelChoose",
        type: 2,
        title: title,
        shadeClose: true,
        shade: 0.5,
        maxmin: true, //开启最大化最小化按钮
        area: ['100%', '100%'],
        content: content
    });
}


//	字典
function getdatadictionary(sss)
{
    $.get(baseURL + "api/bdatadictionary/select?d_code="+sss, function(r){
        if(sss=="material_model"){
            material_model=r.returnlist;
        }else if (sss == "information_type") {
            information_type = r.returnlist;
        } else if (sss == "voltage_type") {
            voltage_type = r.returnlist;
        } else if (sss == "buy_type") {
            buy_type = r.returnlist;
        }
        vm.material_model=material_model;
        vm.information_type=information_type;
        vm.voltage_type=voltage_type;
        vm.buy_type=buy_type;
    });
}


var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
        q:{
            mtype:null

        },
		dMaterialModel: {},
        material_model:material_model
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.dMaterialModel = {};
		},
		update: function (event) {
			var guid = getSelectedRow();
			if(guid == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";

            vm.getInfo(guid)
		},
		saveOrUpdate: function (event) {
			var url = vm.dMaterialModel.guid == null ? "bss/dmaterialmodel/save" : "bss/dmaterialmodel/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.dMaterialModel),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var guids = getSelectedRows();
			if(guids == null){
				return ;
			}

			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "bss/dmaterialmodelitem/delete",
                    contentType: "application/json",
				    data: JSON.stringify(guids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(guid){
			$.get(baseURL + "bss/dmaterialmodel/info/"+guid, function(r){
                vm.dMaterialModel = r.dMaterialModel;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
                postData:{
                    'mtype': vm.q.mtype,
                },
                page:page
            }).trigger("reloadGrid");
		}
	}
});